<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">

            <a-form-item label="批次" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-tooltip>
                <template slot="title">
                  第7批次格式：7
                </template>
                <a-input v-decorator="[ 'batch', validatorRules.batch]" placeholder="请输入批次"></a-input>
              </a-tooltip>

            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'loanName', validatorRules.loanName]" placeholder="请输入姓名"></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'idNumber', validatorRules.idNumber]" placeholder="请输入身份证号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'phone', validatorRules.phone]" placeholder="请输入联系方式"></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="家庭住址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'address', validatorRules.address]" placeholder="请输入家庭住址"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="信用评分" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'creditScore', validatorRules.creditScore]" placeholder="请输入信用评分"></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="信用等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['creditRank',validatorRules.creditRank]"
                                 :trigger-change="true" dictCode="credit_rating" placeholder="请选择信用等级"/>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="贷款用途" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'loanUse', validatorRules.loanUse]" placeholder="请输入贷款用途"></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="户借、户用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isHuyong',validatorRules.isHuyong]" :trigger-change="true"
                                 dictCode="is_huyong" placeholder="请选择是否“户借、户用”"/>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-tooltip>
                <template slot="title">
                  5万格式：5 &nbsp &nbsp 单位:万元
                </template>
                <a-input v-decorator="[ 'money', validatorRules.money]" placeholder="请输入金额"></a-input>
              </a-tooltip>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="贷款银行类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['loanBankType',validatorRules.loanBankType]" :trigger-change="true"
                                 dictCode="bank_type" placeholder="请选择贷款银行类型"/>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="放款银行" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'loanBankId', validatorRules.loanBankId]" placeholder="请输入放款银行"></a-input>
              <!--<a-cascader
                :options="options"
                style="width: 100%"
                @change="onChange"
                :value="cascaderValue"
                placeholder="请选择放款银行"
              />-->
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="期限" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-tooltip>
                <template slot="title">
                  3年格式：36
                </template>
                <a-input v-decorator="[ 'term', validatorRules.term]" placeholder="请输入期限"></a-input>
              </a-tooltip>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
          </a-col>
        </a-row>
        <!--<a-form-item label="信用等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'creditRank', validatorRules.creditRank]" placeholder="请输入信用等级"></a-input>
        </a-form-item>-->
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

    import {httpAction, getAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JDictSelectTag from "@/components/dict/JDictSelectTag"

    export default {
        name: "SmallRecommendationAccountModal",
        components: {
            JDictSelectTag,
        },
        data() {
            return {
                cascaderValue: [],
                options: [],
                processId:"",
                form: this.$form.createForm(this),
                title: "操作",
                width: 1000,
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },

                confirmLoading: false,
                validatorRules: {
                    batch: {rules: [{ required: true, message: '批次不能为空'}]},
                    loanName: {rules: [{ required: true, message: '姓名不能为空'}]},
                    idNumber: {rules: [{ required: true, message: '身份证号不能为空'}]},
                    phone: {rules: [{ required: true, message: '联系方式不能为空'}]},
                    address: {rules: [{ required: true, message: '家庭住址不能为空'}]},
                    creditScore: {rules: [{ required: true, message: '信用评分不能为空'}]},
                    creditRank: {rules: [{ required: true, message: '信用等级不能为空'}]},
                    loanUse: {rules: [{ required: true, message: '贷款用途不能为空'}]},
                    isHuyong: {rules: [{ required: true, message: '选择是否户借户用'}]},
                    money: {rules: [{ required: true, message: '借款金额不能为空'}]},
                    term: {rules: [{ required: true, message: '期限不能为空'}]},
                    bankName: {rules: [{ required: true, message: '放款银行不能为空'}]},
                    loanBankType:{rules: [{ required: true, message: '银行类型不能为空'}]},
                },
                url: {
                    add: "/loanmanagement/smallRecommendationAccount/add",
                    edit: "/loanmanagement/smallRecommendationAccount/edit",
                    queryByProcessId: "/loanmanagement/smallRecommendationAccount/queryByProcessId",
                }

            }
        },
        created() {
        },
        methods: {
            onChange(value, selectedOptions) {
                if (value.length > 0) {
                    this.cascaderValue = value
                    this.loanBankId = selectedOptions[1].code;
                    this.loanBankType=selectedOptions[0].code;
                }
            },
            add() {
                this.edit({});
            },
            edit(record) {
               /* const that = this
                getAction("/common/dictBank/bankCascadedDropDown", "").then((res) => {
                    if (res.success) {
                        this.options = res.result
                    }
                    this.loading = false;
                })
                if (JSON.stringify(record) != "{}") {
                    var params = {};
                    params.loanBankId = record.loanBankId;
                    getAction("/common/dictBank/getCascadedDropDownEchoResults", params).then((res) => {
                        if (res.success) {
                            that.cascaderValue = res.result
                        }
                        this.loading = false;
                    })
                }*/
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'batch', 'loanName', 'idNumber', 'phone', 'address', 'loanBankType', 'creditScore', 'creditRank', 'loanUse', 'isHuyong', 'money', 'term', 'loanBankId', 'processId'))
                })
            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true;
                        let httpurl = '';
                        let method = '';
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        values.processId=this.processId;
                        // values.loanBankId=this.loanBankId;
                        // values.loanBankType=this.loanBankType;
                        let formData = Object.assign(this.model, values);
                        console.log("表单提交数据", formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })
                    }

                })
            },
            handleCancel() {
                this.close()
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row, 'batch', 'loanName', 'idNumber', 'phone', 'address','loanBankType', 'creditScore', 'creditRank', 'loanUse', 'isHuyong', 'money', 'term', 'loanBankId'))
            },
            showGoodsGallery1(record) {
                this.processId = record;
                const that = this;
                var params = {};
                params.processId = record;
                getAction(this.url.queryByProcessId, params).then((res) => {
                    if (res.success) {
                        that.$message.success("回显数据");
                        // that.$emit('ok');
                        that.edit(res.result);
                    } else {
                        that.$message.warning("新增页面");
                        that.add();
                    }
                    this.loading = false;
                })
                this.visible = true
            },

        }
    }
</script>